﻿google.load('visualization', '1', { 'packages': ['corechart','geochart'] });


//google.setOnLoadCallback(drawChart);
$(function () {


    //SetChartGeo('');
    //drawChart();
    SetUI();
    on_update_panel_server(function () {
        //SetChartGeo('');
        SetUI();

    });

    $("#map_reset_world").click(function () {
        //SetChartGeo('');
        DrawChartLocation();
    });

});

function SetUI() {

    $("#TxtBeginDate").datepicker({
        dateFormat: 'yy-mm-dd',
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onClose: function (selectedDate) {
            $("#TxtEndDate").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#TxtEndDate").datepicker({
        dateFormat: 'yy-mm-dd',
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onClose: function (selectedDate) {
            $("#TxtBeginDate").datepicker("option", "maxDate", selectedDate);
        }
    });

    $('h5.titolo-cassetto').click(function () {
        $(this).next().slideToggle();
    });

    $('#calendar_view').click(function () {
        $('div#calendar-frame').toggle();
        return false
    });

}

function SetChartLocation(c) {

    PageMethods.set_path('/App/dashboard.aspx')
    PageMethods.get_geo_data(c,function OnSucceeded(result, usercontext, methodName) {
        //DrawChartLocation(jQuery.parseJSON(result));
        DrawChartLocation(result);
    });
}

function DrawChartLocation(o) {

    var data = new google.visualization.arrayToDataTable(o.data);
    var options = {
        region: o.region,
        displayMode: o.displayMode
    };


    if (o.total > 0) {
        $("#LblMapCount").html(o.total);
    }

    var chart = new google.visualization.GeoChart(document.getElementById('chart_map_country'));
    chart.draw(data, options);

    google.visualization.events.addListener(
        chart, 'regionClick', function (e) {
            //alert(e['region']);

            //var rowindex = data.getFilteredRows([{ column: 0, value: e['region']}]);
            //var location = data.getValue(rowindex[0], 1);
            //location.href = "?ISO=" + e['region'] + "&Location=" + location;

            //alert(location);
            SetChartLocation(e['region'])

        });


}

function DrawChartActivity(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        isStacked: true,
        hAxis: { slantedText: false, minTextSpacing: 0, maxAlternation: 1, maxTextLines: 1},
        series: [
            { color: '#FFD203', type: 'bars' }
        ]

    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_foll_act_div'));
    chart.draw(data, options);
}

function DrawChartFollowerCount(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: '',
        hAxis: { slantedText: true, minTextSpacing: 0, maxAlternation: 1, maxTextLines: 22 },
        series: [
            { color: '#29AAE3', type: 'bars' }
        ]

    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_foll_count'));
    chart.draw(data, options);
}
function DrawChartLanguage(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '4%', top: '4%', width: "92%", height: "92%" },
        colors: ["#C2272D", "#29AAE3", "#8CC63E", "#FFD203", "#662E91"],
        pieHole: 0.4,
        pieSliceText: 'label',
        pieSliceTextStyle: { color: '#ffffff', fontName: 'arial', fontSize: 15 },
        legend:'none',
        tooltip: {
            showColorCode: true, text: 'percentage',
            textStyle: { color: '#000000', fontName: 'arial', fontSize: 15 }
        }

    };

    //legend: {
    //    maxLines: 0, aposition: 'labeled', alignment: 'center',
    //    textStyle: { color: '#000000', fontName: 'arial', fontSize: 15 }
    //},


    var chart = new google.visualization.PieChart(document.getElementById('chart_div_language'));
    chart.draw(data, options);

}

function DrawChartEngagImpress(o, o2) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        lineWidth: 3, pointSize: 7, curveType: '',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#29ABE2' }
        ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div_engag'));
    chart.draw(data, options);

    var data2 = google.visualization.arrayToDataTable(o2.data);

    var options2 = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        lineWidth: 3, pointSize: 7, curveType: '',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#67B203' }
        ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart2 = new google.visualization.LineChart(document.getElementById('chart_div_impress'));
    chart2.draw(data2, options2);

}

function DrawChartPostSend(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        lineWidth: 3, pointSize: 7, curveType: '',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#29ABE2' }
        ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div_post_send'));
    chart.draw(data, options);

}

function DrawChartFollower(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        lineWidth: 3, pointSize: 7, curveType: '',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#67B203' },
            { color: '#C1272D', type: 'bars',targetAxisIndex:1 },
        ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div_follower'));
    chart.draw(data, options);

}

function DrawChartMessShare(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        isStacked: true,
        lineWidth: 3, pointSize: 7, curveType: 'function',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#FF9900' },
            { color: '#C1272D' },
        { color: '#FFD203' }
    ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

function DrawChartStatus(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    //data.setColumnProperty(1, 'role', 'tooltip');

    var formatter = new google.visualization.NumberFormat({ suffix: '%',fractionDigits:0 });
    formatter.format(data, 1); 
    formatter.format(data, 2);
    formatter.format(data, 3);
    formatter.format(data, 4);
    formatter.format(data, 5);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        isStacked: true,
        lineWidth: 3, pointSize: 7, curveType: 'function',
        hAxis: { slantedText: false, maxAlternation: 1, maxTextLines: 1 },
        vAxis: {},
        series: [
            { color: '#C1272D' },
            { color: '#ff6a00' },
            { color: '#67B203',lineWidth: 1, pointSize: 2 },
            { color: '#67B203', lineWidth: 1, pointSize: 3 },
            { color: '#67B203' }
        ],
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div_status'));
    chart.draw(data, options);
}

function DrawChartAutoFollow(o) {
    var data = google.visualization.arrayToDataTable(o.data);

    var options = {
        chartArea: { left: '8%', top: '5%', width: "90%", height: "80%" },
        focusTarget: 'category',
        isStacked: true,
        hAxis: { slantedText: false, minTextSpacing: 0, maxAlternation: 1, maxTextLines: 1 },
        series: [
            { color: '#C1272D', type: 'line', lineWidth: 3, pointSize: 7, curveType: '' },
            { color: '#67B203', type: 'bars' },
            { color: '#FFD203', type: 'bars' }
        ],
        bar: { groupWidth: '80%' },
        legend: { maxLines: 1, position: 'bottom', alignment: 'start' }

    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_auto_foll_div'));
    chart.draw(data, options);
}


